package com.hhq.controller;

import com.hhq.entity.Employee;
import com.hhq.service.Employeeservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class EmployeeController {
    @Autowired
   private Employeeservice employeeservice;

    @RequestMapping("/getAllEmployee")
    public String getAllEmployee(Model model){
        List<Employee> list = employeeservice.getAllEmployee();
        model.addAttribute("requestScopelist",list);
        for (Employee employee : list)
            System.out.println(employee);

        return "employee/list";
    }

    @RequestMapping("/findEmployeeByName")
    public String findEmployeeByName(Model model, String name, HttpServletRequest request){
        List<Employee> list = employeeservice.findEmployeeByName(name);
        System.out.println(list);
        if (list.size()!=0){
            model.addAttribute("requestScopelist",list);
            return "employee/list";
        }
        else {
            request.setAttribute("message","不存在该用户，请重新输入！");
            return "forward:/getAllEmployee";
        }
    }

    @RequestMapping("/toAddEmployee")
    public String toAddEmployee(){
        return "employee/add";
    }

    @RequestMapping("/addEmployee")
    public  String addEmployee(Employee employee){
        int num = employeeservice.addEmployee(employee);
        System.out.println(num);
        return "forward:/getAllEmployee";
    }

    @RequestMapping("/deleteEmployee")
    public  String deleteEmployee(String name){
        employeeservice.deleteEmployee(name);
        System.out.println("Yes");
        return "forward:/getAllEmployee";
    }
    @RequestMapping("/toeditEmployee")
    public String toEmployee(HttpSession session, String name){
        System.out.println(name);
        List<Employee> list = employeeservice.findEmployeeByName(name);
        Employee employee =list.get(0);
        session.setAttribute("employee",employee);
        return "employee/editEmployee";
    }

    @RequestMapping("/updateEmployee")
    public String updateEmployee(Employee employee){
        System.out.println(employee);
        int num = employeeservice.updateEmployee(employee);
        System.out.println(num);
        return "forward:/getAllEmployee";
    }

}
